{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True, False, False], dtype=bool)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy\n",
    "#it will compare the second value to each element in the vector\n",
    "# If the values are equal, the Python interpreter returns True; otherwise, it returns False\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "vector == 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False],\n",
       "       [False,  True, False],\n",
       "       [False, False, False]], dtype=bool)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "matrix == 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False False]\n",
      "[10]\n"
     ]
    }
   ],
   "source": [
    "#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "equal_to_ten = (vector == 10)\n",
    "print equal_to_ten\n",
    "print(vector[equal_to_ten])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False]\n",
      "[[20 25 30]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                [5, 10, 15], \n",
    "                [20, 25, 30],\n",
    "                [35, 40, 45]\n",
    "             ])\n",
    "second_column_25 = (matrix[:,1] == 25)\n",
    "print second_column_25\n",
    "print(matrix[second_column_25, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False False]\n"
     ]
    }
   ],
   "source": [
    "#We can also perform comparisons with multiple conditions\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "equal_to_ten_and_five = (vector == 10) & (vector == 5)\n",
    "print equal_to_ten_and_five"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True False False]\n"
     ]
    }
   ],
   "source": [
    "vector = numpy.array([5, 10, 15, 20])\n",
    "equal_to_ten_or_five = (vector == 10) | (vector == 5)\n",
    "print equal_to_ten_or_five"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[50 50 15 20]\n"
     ]
    }
   ],
   "source": [
    "vector = numpy.array([5, 10, 15, 20])\n",
    "equal_to_ten_or_five = (vector == 10) | (vector == 5)\n",
    "vector[equal_to_ten_or_five] = 50\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False]\n",
      "[[ 5 10 15]\n",
      " [20 10 30]\n",
      " [35 40 45]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "            [5, 10, 15], \n",
    "            [20, 25, 30],\n",
    "            [35, 40, 45]\n",
    "         ])\n",
    "second_column_25 = matrix[:,1] == 25\n",
    "print second_column_25\n",
    "matrix[second_column_25, 1] = 10\n",
    "print matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|S1\n",
      "['1' '2' '3']\n",
      "float64\n",
      "[ 1.  2.  3.]\n"
     ]
    }
   ],
   "source": [
    "#We can convert the data type of an array with the ndarray.astype() method.\n",
    "vector = numpy.array([\"1\", \"2\", \"3\"])\n",
    "print (vector.dtype)\n",
    "print vector\n",
    "vector = vector.astype(float)\n",
    "print vector.dtype\n",
    "print vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vector = numpy.array([5, 10, 15, 20])\n",
    "vector.min()\n",
    "#print (help(numpy.array))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  75, 120])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The axis dictates which dimension we perform the operation on\n",
    "#1 means that we want to perform the operation on each row, and 0 means on each column\n",
    "matrix = numpy.array([\n",
    "                [5, 10, 15], \n",
    "                [20, 25, 30],\n",
    "                [35, 40, 45]\n",
    "             ])\n",
    "matrix.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([60, 75, 90])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                [5, 10, 15], \n",
    "                [20, 25, 30],\n",
    "                [35, 40, 45]\n",
    "             ])\n",
    "matrix.sum(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[             nan              nan              nan              nan\n",
      "               nan]\n",
      " [  1.98600000e+03              nan              nan              nan\n",
      "    0.00000000e+00]\n",
      " [  1.98600000e+03              nan              nan              nan\n",
      "    5.00000000e-01]\n",
      " ..., \n",
      " [  1.98700000e+03              nan              nan              nan\n",
      "    7.50000000e-01]\n",
      " [  1.98900000e+03              nan              nan              nan\n",
      "    1.50000000e+00]\n",
      " [  1.98500000e+03              nan              nan              nan\n",
      "    3.10000000e-01]]\n",
      "[ True False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False  True False False False False  True False False False False False\n",
      " False False False False False False False False False  True False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      "  True False False False False False False False False False  True False\n",
      " False False False False False False  True False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False  True  True False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False  True False False\n",
      " False False False False False False False False False False False False\n",
      " False False False  True False False False False False  True False False\n",
      " False False False  True False False False False False False False False\n",
      "  True False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False  True False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False  True False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False  True False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False  True False False False False False False False\n",
      " False False False False False False False False False False  True False\n",
      " False False  True False False False False False False False False False\n",
      " False False False False False False  True False False False False False\n",
      " False False False False False False  True False  True  True False False\n",
      " False False False  True False False False False False  True False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False  True False False\n",
      " False False False False False False False False  True False False False\n",
      " False False False False False False False False False False  True False\n",
      " False False False False  True False False  True False False False False\n",
      " False  True False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False  True  True False False False False\n",
      " False  True False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      "  True False False False False False  True False False  True False False\n",
      "  True False False  True False False False False False False False False\n",
      " False False False False  True False False False False False False False\n",
      " False False False False False False False False False False False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False  True False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False  True  True False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False  True False False False  True False False False False\n",
      " False False False False False False False False False False False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False False False  True False False False False False  True\n",
      " False False False False False False False False False False False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False  True False False False False False False False False False\n",
      "  True  True False  True False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False  True\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False  True  True False  True False False\n",
      "  True  True False False False False  True False False False False False\n",
      " False False False False False False False False False  True False False\n",
      " False False False False False False False False False False False False\n",
      " False False  True False False False False False False False  True False\n",
      " False  True False False False False False False False False False False\n",
      " False False]\n",
      "1137.78\n",
      "1.14006012024\n"
     ]
    }
   ],
   "source": [
    "#replace nan value with 0\n",
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\",dtype=float)\n",
    "print (world_alcohol)\n",
    "is_value_empty = numpy.isnan(world_alcohol[:,4])\n",
    "print (is_value_empty)\n",
    "world_alcohol[is_value_empty, 4] = '0'\n",
    "alcohol_consumption = world_alcohol[:,4]\n",
    "alcohol_consumption = alcohol_consumption.astype(float)\n",
    "total_alcohol = alcohol_consumption.sum()\n",
    "average_alcohol = alcohol_consumption.mean()\n",
    "print (total_alcohol)\n",
    "print (average_alcohol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
